%this file loads a single acoustic capture file that contains captures for
%all tags, then writes out individual capture files that can be used with
%patch captures.

%initalize parameters, change settings here
windowMinutes=60; %in minutes.  If there is a gap in the detection events 
%greater than this number then the detections will be seperated into two
%different events.


%housekeeping
windowDays=windowMinutes/(24*60);


%load the acoustic captures file (output by labview cleaning program)
[fname,fpath]=uigetfile('*.csv','select the acoustic captures spreadsheet');
captures=dlmread(strcat(fpath,fname),',');
captures(:,1)=captures(:,1)-(4/24); %this line converts the captures data 
%from gmt to edt, if you don't want to make this conversion comment out
%this line.

%prompt user to create the base file name and path for the dtections files
[fname,fpath]=uiputfile('*.csv','select basepath for detections files');



%create an array of the unique tag values, we will loop through this to
%create individual tag captures

tags=unique(captures(:,2));
whTag=waitbar(0,'percent of tags processed');

%loop through the tags array
for t=1:length(tags)
    thisTagInd=find(captures(:,2)==tags(t));
    thisTag=captures(thisTagInd,:);
    thisTag=sortrows(thisTag,1);
    
    [nHits,nu]=size(thisTag);
    %now we need to loop through each detection for this tag and create
    %agregate detection events from discrete detections
    events=zeros(1,5);
    startHit=thisTag(1,:);
    lastTime=startHit(1);
    stopHit=thisTag(1,:);
    e=1;
    whHit=waitbar(0,strcat('Percent of tag ',num2str(startHit(2)),'-detections processed'));
    for i=1:nHits
        
        if  i<nHits && (thisTag(i,3)==startHit(3)) && ((thisTag(i,1)-lastTime)<=windowDays)
            %if this detection has the same station as the detection we are
            %working on, and if it is within the time window, incriment
            %lastTime and stopHit and move on
            lastTime=thisTag(i,1);
            stopHit=thisTag(i,:);
        else
            %record event
            events(e,1)=startHit(1);
            events(e,2)=stopHit(1);
            events(e,3)=stopHit(1)-startHit(1);
            events(e,4)=startHit(2);
            events(e,5)=startHit(3);
            %increment event address
            e=e+1;            
            %reset start and stop hits and times
            lastTime=thisTag(i,1);
            startHit=thisTag(i,:);
            stopHit=thisTag(i,:);
        end
        waitbar(i/nHits,whHit);
    end
    close(whHit);
    dlmwrite(strcat(fpath,fname(1:length(fname)),'_detections_',num2str(startHit(2)),'.csv'), events,'delimiter',',','precision',15);
    waitbar(t/length(tags),whTag);
end
close(whTag);
       
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            